/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	APatrolServiceImpl.java
 * Created：		2016年8月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.patrol;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.patrol.converter.ADeviceCategoryConverter;
import com.thor.palmmall.rest.patrol.converter.ADeviceConverter;
import com.thor.palmmall.rest.patrol.converter.APatrolBillConverter;
import com.thor.palmmall.rest.patrol.converter.APatrolItemConverter;
import com.thor.palmmall.rest.patrol.converter.SDeviceInspect4SaveConverter;
import com.thor.palmmall.rest.patrol.converter.SDeviceInspectConverter;
import com.thor.palmmall.rest.patrol.converter.SPatrolQueryParamConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.basic.SDeviceCategory;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.device.SDevice;
import com.thor.palmmall.spi.device.SDeviceCategoryField;
import com.thor.palmmall.spi.device.SDeviceInspect;
import com.thor.palmmall.spi.device.SDeviceService;

/**
 * @author wolv
 *
 */
public class APatrolServiceImpl implements APatrolService {

  @Override
  public List<ADevice> queryDevices(String storeUuid, String categoryUuid)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(storeUuid, "storeUuid");
    Assert.assertArgumentNotNull(categoryUuid, "categoryUuid");
    SResponse<List<SDevice>> resp = sBasicService.queryDevice(storeUuid, categoryUuid);
    if (resp.isSuccess())
      return ConverterUtil.convert(resp.getBody(), ADeviceConverter.getInstance());
    else
      throw new ThorServiceException("无法查询设备：" + resp.getMessage());
  }

  @Override
  public ADevice getDevice(String uuid) throws ThorServiceException {
    Assert.assertArgumentNotNull(uuid, "uuid");

    SResponse<SDevice> resp = sBasicService.getDevice(uuid);
    if (resp.isSuccess())
      return ADeviceConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法查询设备：" + resp.getMessage());
  }

  @Override
  public List<APatrolItem> queryPatrolItems(String category,
      boolean isMaintenance) throws ThorServiceException {
    Assert.assertArgumentNotNull(category, "category");

    SResponse<List<SDeviceCategoryField>> resp = sBasicService.queryInspectItems(category,
        isMaintenance);
    if (resp.isSuccess())
      return ConverterUtil.convert(resp.getBody(), APatrolItemConverter.getInstance());
    else
      throw new ThorServiceException("无法查询设备：" + resp.getMessage());
  }

  @Override
  public String create(APatrolBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceService.create(
        SDeviceInspectConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建巡检单：" + resp.getMessage());
  }

  @Override
  public void save(APatrolBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<Void> resp = sDeviceService.save(
        SDeviceInspect4SaveConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return;
    else
      throw new ThorServiceException("无法保存巡检单：" + resp.getMessage());
  }

  @Override
  public QueryResult<APatrolBill> query(APatrolQueryParam filter) throws ThorServiceException {
    Assert.assertArgumentNotNull(filter, "filter");

    SResponse<SQueryResult<SDeviceInspect>> resp = sDeviceService
        .query(SPatrolQueryParamConverter.getInstance().convert(filter));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), APatrolBillConverter.getInstance());
    else
      throw new ThorServiceException("无法查询巡检单：" + resp.getMessage());
  }

  @Override
  public APatrolBill get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SDeviceInspect> resp = sDeviceService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return APatrolBillConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的巡检单：" + resp.getMessage());
  }

  @Override
  public List<ADeviceCategory> queryCategorys(boolean isMaintenance, boolean isInspect)
      throws ThorServiceException {
    SResponse<List<SDeviceCategory>> resp = sBasicService.queryCategorys(isMaintenance, isInspect);
    if (resp.isSuccess())
      return ConverterUtil.convert(resp.getBody(), ADeviceCategoryConverter.getInstance());
    else
      throw new ThorServiceException("无法查询巡检设备类型：" + resp.getMessage());
  }

  @Autowired
  private SDeviceService sDeviceService;

  @Autowired
  private SBasicService sBasicService;
  
}
